<?php

namespace App\Func\Wx;

use App\ApiCall\Wx\Applet\WxAppletExecute;
use App\ApiCall\Wx\Qy\WxQyExecute;
use Illuminate\Support\Facades\Redis;

class WxQyFunc
{
    public function getAccessToken(){
        $key = config('openapi.wx.qy_wx.qy_wx_access_token_key');
        $ttl = Redis::ttl($key);
        if(!$ttl||$ttl<=300){
            $res = (new WxQyExecute())->getAccessToken();
            $fmtRes = json_decode($res->body());
            if(isset($fmtRes->errcode) && $fmtRes->errcode!=0){
                s_fail_return(null,$fmtRes->errmsg);
            }
            Redis::setex($key, $fmtRes->expires_in, $fmtRes->access_token);
            return $fmtRes->access_token;
        }else{
            return Redis::get($key);
        }
    }
}
